package cn.xinfei.xdecision.common.model.login;

import cn.xinfei.xdecision.common.utils.exception.ApiException;
import cn.xinfei.xdecision.common.utils.exception.enums.ErrorCodeEnum;
import com.alibaba.ttl.TransmittableThreadLocal;

/**
 * session管理类
 */
public class SessionManager {

    private SessionManager() {}

    private static final TransmittableThreadLocal<AccountSessionWrap> session = new TransmittableThreadLocal<>();

    public static AccountSessionWrap getSession() {
        return session.get();
    }

    public static void setSession(AccountSessionWrap conn) {
        session.set(conn);
    }

    public static SSOUser getLoginAccount() {
        if (getSession() != null) {
            return getSession().getSsoUser();
        } else {
            throw new ApiException(ErrorCodeEnum.ERROR_TOKEN_EXPIRE);
        }
    }

    public static Long getLoginUserId() {
        SSOUser loginAccount = getLoginAccount();
        return loginAccount != null ? loginAccount.getId() : null;
    }
}